package com.ruoyi.workSafetyStandardization.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.workSafetyStandardization.Vo.inspection.InspectionAddVo;
import com.ruoyi.workSafetyStandardization.domain.*;
import com.ruoyi.workSafetyStandardization.dto.inspection.InspectionResultDto;
import com.ruoyi.workSafetyStandardization.dto.inspection.MajorDto;
import com.ruoyi.workSafetyStandardization.service.IInspectionMajorResultService;
import com.ruoyi.workSafetyStandardization.service.IInspectionTeamMembersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.workSafetyStandardization.mapper.InspectionResultMapper;
import com.ruoyi.workSafetyStandardization.service.IInspectionResultService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 标准化考核汇总Service业务层处理
 *
 * @author pengjie
 * @date 2023-12-15
 */
@Service
public class InspectionResultServiceImpl implements IInspectionResultService
{
    @Autowired
    private InspectionResultMapper inspectionResultMapper;
    @Autowired
    private IInspectionTeamMembersService teamMembersService;
    @Autowired
    private IInspectionMajorResultService majorResultService;

    /**
     * 查询标准化考核汇总
     *
     * @param inspectionId 标准化考核汇总主键
     * @return 标准化考核汇总
     */
    @Override
    public InspectionResult selectInspectionResultByInspectionId(String inspectionId)
    {
        return inspectionResultMapper.selectInspectionResultByInspectionId(inspectionId);
    }

    /**
     * 查询标准化考核汇总列表
     *
     * @param inspectionResult 标准化考核汇总
     * @return 标准化考核汇总
     */
    @Override
    public List<InspectionResult> selectInspectionResultList(InspectionResult inspectionResult)
    {
        return inspectionResultMapper.selectInspectionResultList(inspectionResult);
    }

    /**
     * 新增标准化考核汇总
     *
     * @param inspectionResult 标准化考核汇总
     * @return 结果
     */
    @Override
    public int insertInspectionResult(InspectionResult inspectionResult)
    {
        return inspectionResultMapper.insertInspectionResult(inspectionResult);
    }

    /**
     * 修改标准化考核汇总
     *
     * @param inspectionResult 标准化考核汇总
     * @return 结果
     */
    @Override
    public int updateInspectionResult(InspectionResult inspectionResult)
    {
        return inspectionResultMapper.updateInspectionResult(inspectionResult);
    }

    /**
     * 批量删除标准化考核汇总
     *
     * @param inspectionIds 需要删除的标准化考核汇总主键
     * @return 结果
     */
    @Override
    public int deleteInspectionResultByInspectionIds(String[] inspectionIds)
    {
        return inspectionResultMapper.deleteInspectionResultByInspectionIds(inspectionIds);
    }

    /**
     * 删除标准化考核汇总信息
     *
     * @param inspectionId 标准化考核汇总主键
     * @return 结果
     */
    @Override
    public int deleteInspectionResultByInspectionId(String inspectionId)
    {
        return inspectionResultMapper.deleteInspectionResultByInspectionId(inspectionId);
    }

    /**
     * 通过时间,查询标准化考核汇列表
     *
     * @param inspectionResult
     * @return 结果
     */
    @Override
    public List<InspectionResult> queryInspectionResultListByDate(InspectionResult inspectionResult) {
        return inspectionResultMapper.queryInspectionResultListByDate( inspectionResult);
    }

    @Override
    public InspectionResultDto queryInspectionResultListByInspectionId(String inspectionId) {
        //查询InspectionResult
        InspectionResult inspectionResult=inspectionResultMapper.selectInspectionResultByInspectionId(inspectionId);
        //封装---主体
        InspectionResultDto inspectionResultDto=new InspectionResultDto();
        BeanUtils.copyProperties(inspectionResult,inspectionResultDto);
        //封装---leaderList
        List<InspectionTeamMembers> leaderList=teamMembersService.queryLeaderListByInspectionId(inspectionId);
        inspectionResultDto.setLeaderList(leaderList);
        //封装---majorList
        List<InspectionMajorResult> majorList=majorResultService.queryMajorListByInspectionId(inspectionId);
//        List<MajorDto> majorDtoList=new LinkedList<>();
//        BeanUtils.copyProperties(majorList,majorDtoList);
//        System.out.println(majorList.isEmpty());
//        System.out.println(majorDtoList.isEmpty());

        //查询全部majorResult
        InspectionMajorResult inspectionMajorResult=new InspectionMajorResult();
        inspectionMajorResult.setInspectionId(inspectionId);
        List<InspectionMajorResult> majorResultList=majorResultService.selectInspectionMajorResultList(inspectionMajorResult);
        List<MajorDto> majorDtoList1=majorList.stream().filter(at->"".equals(at.getParentId())).map(it->{
//            List<InspectionMajorResult> inspectionMajorResultList=majorResultService.queryChildrenByParentId(it.getMajorId());
            List<MajorDto> list1=majorResultService.getChildrenTree(it.getMajorId(),majorResultList);
            MajorDto majorDto=new MajorDto();
            BeanUtils.copyProperties(it,majorDto);
            majorDto.setChildren(list1);
            List<InspectionTeamMembers> leaderList1=teamMembersService.queryLeaderListByInspectionIdAndMajorId(majorDto);
            majorDto.setMajorLeaderList(leaderList1);
            return majorDto;
        }).collect(Collectors.toList());
        inspectionResultDto.setMajorList(majorDtoList1);
        return inspectionResultDto;
    }

    /**
     * 发起评测
     * @param inspectionAddVo
     * @return
     */
    @Transactional
    @Override
    public int addInspectionResult(InspectionAddVo inspectionAddVo) {
        //封装inspectionResult，加入inspectionResult表
        InspectionResult inspectionResult=new InspectionResult();
        BeanUtils.copyProperties(inspectionAddVo,inspectionResult);
        inspectionResult.setCreateByName(SecurityUtils.getUsername());
        inspectionResult.setCreateAt(new Date());
        inspectionResult.setCreateBy(SecurityUtils.getUserId().toString());
            //封装inspectionId
        inspectionResult.setInspectionId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        if (inspectionResult.getInspectionType()==0){
            //封装inspectionNo  (内外评审有不同的编号)
            inspectionResult.setInspectionNo(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        }
        //封装评委领导信息，加入team_member表    (LeaderList)
        inspectionAddVo.getLeaderList().stream().map(at->{
            InspectionTeamMembers teamMembers=new InspectionTeamMembers();
            BeanUtils.copyProperties(at,teamMembers);
            teamMembers.setInspectionMemberId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
            teamMembers.setInspectionId(inspectionResult.getInspectionId());
            return teamMembersService.insertInspectionTeamMembers(teamMembers);
        }).collect(Collectors.toList());
            //(MajorList)
        inspectionAddVo.getMajorList().stream().map(it->{
            //封装专业领导信息，加入team_member表  (MajorLeaderList)
            it.getMajorLeaderList().stream().map(ma-> {
                        InspectionTeamMembers teamMembers = new InspectionTeamMembers();
                        BeanUtils.copyProperties(ma, teamMembers);
            teamMembers.setInspectionMemberId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
                        teamMembers.setInspectionId(inspectionResult.getInspectionId());
                        return teamMembersService.insertInspectionTeamMembers(teamMembers);
                    }
            ).collect(Collectors.toList());
            //封装专业参与人信息，加入team_member表  (MajorParticipantVoList)
            it.getMajorParticipantVoList().stream().map(na-> {
                        InspectionTeamMembers teamMembers = new InspectionTeamMembers();
                        BeanUtils.copyProperties(na, teamMembers);
            teamMembers.setInspectionMemberId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
                        teamMembers.setInspectionId(inspectionResult.getInspectionId());
                        return teamMembersService.insertInspectionTeamMembers(teamMembers);
                    }
            ).collect(Collectors.toList());

            return true;
        }).collect(Collectors.toList());

        return inspectionResultMapper.insertInspectionResult(inspectionResult);
    }
}
